/*
This file is part of Docking@Home.
http://docking.gcl.cis.udel.edu
Copyright (C) 2009 University of Delaware

Docking@Home is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Docking@Home is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Docking@Home.  If not, see <http://www.gnu.org/licenses/>.
*/
/*
	Filename: Tab.cpp
	Description: An abstract class (aka interface) that contains all necessary
		functions that any type of tab must implement. All non-virtual functions
		are implemented in this .cpp file
	Revision History: 
		Created - 01/03/08
	Author: Robert Keller (01/03/08 -> current)
*/

#include "Tab.h"

// Must be included for memcpy to work ?!?!?!
#include "parse.h"

// ------------------------------------------------------------
// Static Variable Declarations
// ------------------------------------------------------------

GLHelper * Tab::glHelper;
TabWindow * Tab::tabWindow;

const float Tab::TAB_WIDTH(120);
const float Tab::TAB_HEIGHT(25);
const float Tab::TAB_SPACING(10);
const float Tab::TAB_X_SCALE_FACTOR(0.65f);
const float Tab::TAB_Y_SCALE_FACTOR(-0.65f);

// ------------------------------------------------------------
// Constructor
// ------------------------------------------------------------

Tab::Tab () 
{
	nameIndentX = 0.0f;
	nameIndentY = 0.0f;

	state = UNSELECTED;
	bSelected = false;

	lineSpacing = 0.0f;

}// Tab

// ------------------------------------------------------------
// Static Member Modifiers
// ------------------------------------------------------------

void Tab::setTabWindow (TabWindow * t) 
{ 
	tabWindow = t; 
}// setTabWindow

// ------------------------------------------------------------
// Member Modifiers
// ------------------------------------------------------------

void Tab::setName (const char n[TAB_MAX_NAME_LENGTH])
{
	memcpy(name, n, sizeof(char) * TAB_MAX_NAME_LENGTH);
}// setName

void Tab::setNameIndentX (float x)
{
	nameIndentX = x;
}// setNameIndentX

void Tab::setNameIndentY (float y)
{
	nameIndentY = y;
}// setNameIndentY

// ------------------------------------------------------------
// Set Text Alpha Blending Values
// ------------------------------------------------------------

void Tab::setTextSelectedAlpha (float alpha)
{
	textSelectedAlpha = alpha;
}// setTextSelectedAlpha

void Tab::setTextUnselectedAlpha (float alpha)
{
	textUnselectedAlpha = alpha;
}// setTextUnselectedAlpha

void Tab::setTextHoveredAlpha (float alpha)
{
	textHoveredAlpha = alpha;
}// setTextHoverAlpha

void Tab::select ()
{
	state = SELECTED;
	bSelected = true;
}// select

void Tab::unselect ()
{
	state = UNSELECTED;
	bSelected = false;
}// unselect

// ------------------------------------------------------------
// Color Setting Functions
// ------------------------------------------------------------

void Tab::setSelectedBoxColor(const float color[4])
{
	glHelper->copyColor4f(selectedBoxColor, color);
}// setSelectedBoxColor

void Tab::setUnselectedBoxColor(const float color[4])
{
	glHelper->copyColor4f(unselectedBoxColor, color);
}// setUnselectedBoxColor

void Tab::setHoveredBoxColor(const float color[4])
{
	glHelper->copyColor4f(hoveredBoxColor, color);
}// setHoverBoxColor

void Tab::setSelectedTextColor(const float color[4])
{
	glHelper->copyColor4f(selectedTextColor, color);
}// setSelectedTextColor

void Tab::setUnselectedTextColor(const float color[4])
{
	glHelper->copyColor4f(unselectedTextColor, color);
}// setUnselectedTextColor

void Tab::setTextColor(const float color[4])
{
	glHelper->copyColor4f(textColor, color);
}// setSelectedTextColor

void Tab::setLineSpacing (float ls)
{
	lineSpacing = ls;
}// setLineSpacing

// ------------------------------------------------------------
// State Determination
// ------------------------------------------------------------

bool Tab::determineState (unsigned int index, int curX, 
	int curY, int clicked)
{
	// Starting positions of the tab
	float tabStartX;
	float tabStartY;

	// Determine top left corner of the tab

	getTabStart (index, &tabStartX, &tabStartY);

	if (curX <= tabStartX + width
			&& curX >= tabStartX
			&& curY <= tabStartY + height
			&& curY >= tabStartY)
	{
		// Button was selected
		if (clicked)
		{
			bSelected = true;	
			state = SELECTED;

			return true;
		}// if
		else
			state = HOVER;
	}// if
	else
	{
		(bSelected == true) ? state = SELECTED : state = UNSELECTED;
	}// else

	return false;	
}// determineState

// ------------------------------------------------------------
// Calculate Location Values
// ------------------------------------------------------------

void Tab::getTabStart (unsigned int index, float * tabStartX, 
	float * tabStartY)
{
	width = TAB_WIDTH * tabWindow->getWidthFactor();
	height = TAB_HEIGHT * tabWindow->getHeightFactor();	

	tabScaleTextX = TAB_X_SCALE_FACTOR * tabWindow->getWidthFactor();
	tabScaleTextY = TAB_Y_SCALE_FACTOR * tabWindow->getHeightFactor();

	*tabStartX = (width + TAB_SPACING * tabWindow->getWidthFactor()) 
		* index + tabWindow->getTopLeftX();

	*tabStartY = tabWindow->getTopLeftY() - height;
	
}// getTabStart
